package com.eduplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eduplatform.common.Result;
import com.eduplatform.common.UserContext;
import com.eduplatform.dto.QuestionDTO;
import com.eduplatform.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 试题控制器
 */
@RestController
@RequestMapping("/api/questions")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserContext userContext;

    /**
     * 分页查询试题列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer difficulty,
            @RequestParam(required = false) Long subjectId,
            @RequestParam(required = false) Integer educationLevel,
            @RequestParam(required = false) String keyword) {
        IPage<QuestionDTO> pageResult = questionService.pageQuestions(page, size, type, difficulty,
                subjectId, educationLevel, keyword);

        Map<String, Object> data = new HashMap<>();
        data.put("total", pageResult.getTotal());
        data.put("pages", pageResult.getPages());
        data.put("current", pageResult.getCurrent());
        data.put("records", pageResult.getRecords());

        return Result.success(data);
    }

    /**
     * 获取试题详情
     */
    @GetMapping("/{id}")
    public Result getDetail(@PathVariable Long id) {
        QuestionDTO question = questionService.getQuestionDetail(id);
        if (question == null) {
            return Result.error("试题不存在");
        }
        return Result.success(question);
    }

    /**
     * 添加试题
     */
    @PostMapping("/add")
    public Result add(@RequestBody @Valid QuestionDTO questionDTO) {
        // 设置创建人ID
        Long currentUserId = userContext.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }

        questionDTO.setCreatorId(currentUserId);

        boolean result = questionService.addQuestion(questionDTO);
        return result ? Result.success() : Result.error("添加试题失败");
    }

    /**
     * 更新试题
     */
    @PutMapping("/update")
    public Result update(@RequestBody @Valid QuestionDTO questionDTO) {
        if (questionDTO.getId() == null) {
            return Result.error("试题ID不能为空");
        }

        boolean result = questionService.updateQuestion(questionDTO);
        return result ? Result.success() : Result.error("更新试题失败");
    }

    /**
     * 删除试题
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        boolean result = questionService.deleteQuestion(id);
        return result ? Result.success() : Result.error("删除试题失败");
    }
}